// tabBarComponent/tabBar.js
const app = getApp();
const config = require('../../config/config')
const {
  decodeUserInfo
} = require('../../api/login')
const {
  checkLogin,
  isBind
} = require('../../utils/util')
const {
  scanOpen
} = require('../../api/api')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    tabbar: {
      type: Object,
      value: {
        "backgroundColor": "#ffffff",
        "color": "#979795",
        "selectedColor": "#1c1c1b",
        "list": []
      },
    },
    currentIndex: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    isIphoneX: app.globalData.isIphoneX,
    isOpen: false,
    bottomLift: getApp().globalData.bottomLift,
  },

  lifetimes: {
    attached: function () {

    },
  },
  methods: {
    tab(e) {
      var url = e.currentTarget.dataset.url
      var index = e.currentTarget.dataset.index
      wx.switchTab({
        url: url,
      })
    },

    scan: function () {
      let that = this
      wx.showLoading({
        title: '加载中',
      })
      checkLogin('none').then((res) => {
        wx.scanCode({
          onlyFromCamera: true,
          success(res) {
            let query = getApp().getQueryVariable(res.result, 'n', 'i')
            isBind('deptId').then((res) => {
              that.turn(query)
            }).catch((e) => {
              wx.hideLoading({})
              wx.showToast({
                title: '请先绑定部门',
                icon: 'none'
              })
              setTimeout(() => {
                wx.navigateTo({
                  url: "/pages/users/dept/index?code=" + query.code,
                })
              }, 1000);
            })
          },
          fail: function (err) {
            wx.showToast({
              title: '未获取正常的二维码',
              icon: 'none'
            })
          },
        })

      }).catch((e) => {
        wx.hideLoading({})
        this.triggerEvent('login')
      })
    },
    async turn(query) {
      if (query.type === 'door') {
        const params = {
          doorCode: query.code
        }
        wx.hideLoading()
        const result = await scanOpen(params)
        wx.showToast({
          title: '门已打开',
          icon:'none'
        })
        // wx.navigateTo({
        //   url: "/pages/donation/choose/index?code=" + query.code,
        // })
      } else if (query.type === 'bag') {
        wx.hideLoading()
        const params = {
          bagCode: query.code
        }
        const result = await bagUse(params)
        wx.showToast({
          title: '已领取',
          icon: 'none'
        })
      }
    }

  }
})